package com.atguigu.gmall.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CompletableFutureDemo {
    public static void main(String[] args) {



/*
        for (int x = 0; x < 10; x++) {
            //多线程之间  串行关系
            CompletableFuture.runAsync(() -> {
                for (int i = 0; i < 3; i++) {
                    System.out.println("A");
                }
            }).thenAccept(t -> {
                for (int i = 0; i < 4; i++) {
                    System.out.println("B");
                }
            }).thenAccept(t -> {
                for (int i = 0; i < 5; i++) {
                    System.out.println("C");
                }
            });
        }
*/

 /*       //1:创建一个多线程
        CompletableFuture.runAsync(() ->{
            int i = 0;
            i = 1;
             i++;
            System.out.println(i);
        });
        CompletableFuture.supplyAsync(()->{
            int i = 0;
            i = 1;
            i++;
            System.out.println(i);
            return i;
        });
*/

     /*   //2:计算完成时回调方法
        CompletableFuture.supplyAsync(()->{
            int i = 0;
            i = 1;
            i++;
            System.out.println("i:" + i);
            return i;//获取1号线程
        }).whenCompleteAsync((t,u) ->{
            //降级处理
            System.out.println("t=i" + t);
            System.out.println("u:异常: " + t);
        }).exceptionally((u) ->{
            System.out.println("exceptionally:u:异常: " + u);
            return  123;
        });*/
  /*   //3:串行
        CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("串行1");
            return "123";
        }).thenApplyAsync((t) ->{
            System.out.println("t:" + t);
            return t;
        }).whenCompleteAsync((t,u) -> {
            //降级处理
            System.out.println("whenCompleteAsync:t:" + t);
            System.out.println("whenCompleteAsync:u:" + u);
        }).join();*/

            // 并行
        CompletableFuture<String> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("串行1");
            return "123";
        });
        CompletableFuture<String> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("并行2");
            return "456";
        });
        CompletableFuture<String> completableFuture3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("并行3");
            return "789";
        });

        //Main  等会
        CompletableFuture.anyOf(completableFuture1,completableFuture2,completableFuture3).join();

    }
}